﻿using System;
using System.Collections.Generic;
using System.Linq;
using Eagles.Application.Model;
using Eagles.Application.Model.Organization.Model;
using Eagles.Application.Model.Organization.Requset;
using Eagles.Application.Model.Organization.Response;
using Eagles.Base;
using Eagles.Base.Cache;
using Eagles.Base.Utility;
using Eagles.DomainService.Model.Org;
using Eagles.DomainService.Model.User;
using Eagles.Interface.Core;
using Eagles.Interface.DataAccess;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Utilities;

namespace Eagles.DomainService.Core
{
    public class OrganizationHandler : IOrganizationHandler
    {
        private readonly IOrganizationDataAccess dataAccess;

        private readonly IBranchDataAccess BrndataAccess;

        private readonly ICacheHelper Cache;
        public OrganizationHandler(IOrganizationDataAccess dataAccess, ICacheHelper cache, IBranchDataAccess brndataAccess)
        {
            this.dataAccess = dataAccess;
            Cache = cache;
            BrndataAccess = brndataAccess;
        }

        public bool EditOrganization(EditOrganizationRequset requset)
        {

            var tokenInfo = Cache.GetData<TbUserToken>(requset.Token);

            TbOrgInfo mod;
            var now = DateTime.Now;
            if (requset.Info.OrgId > 0)
            {
                mod = new TbOrgInfo
                {
                    Address = requset.Info.Address,
                    Province = requset.Info.Province,
                    OrgName = requset.Info.OrgName,
                    ScoreType = requset.Info.ScoreType,
                    City = requset.Info.City,
                    District = requset.Info.District,
                    EditTime = now,
                    Logo = requset.Info.Logo,
                    OperId = tokenInfo.UserId,
                    OrgId = requset.Info.OrgId,
                    FeeNotice = requset.Info.FeeNotice,
                    FeeExpireNoticeDay = requset.Info.FeeExpireNoticeDay,
                    Secretary =requset.Info.Secretary,
                    ViceSecretary=requset.Info.ViceSecretary
                };

                return dataAccess.EditOrganization(mod) > 0;


            }
            else
            {
                mod = new TbOrgInfo
                {
                    Address = requset.Info.Address,
                    Province = requset.Info.Province,
                    OrgName = requset.Info.OrgName,
                    City = requset.Info.City,
                    CreateTime = now,
                    District = requset.Info.District,
                    //EditTime = now,
                    Logo = requset.Info.Logo,
                    ScoreType=requset.Info.ScoreType,
                    FeeNotice=requset.Info.FeeNotice,
                    FeeExpireNoticeDay=requset.Info.FeeExpireNoticeDay,
                    OperId = tokenInfo.UserId,
                    Secretary = requset.Info.Secretary,

                    ViceSecretary = requset.Info.ViceSecretary

                };
                return dataAccess.CreateOrganization(mod) > 0;


            }

        }

        public bool RemoveOrganization(RemoveOrganizationRequset requset)
        {

            if (BrndataAccess.GetBranchCountOrgId(requset.OrgId) > 0)
            {
                throw  new TransactionException("M20");
            }

                return dataAccess.RemoveOrganization(requset) > 0;
        }

        public GetOrganizationResponse Organization(GetOrganizationRequset requset)
        {
            var response = new GetOrganizationResponse
            {
                TotalCount = 0,
            };
            List<TbOrgInfo> list = dataAccess.GetOrganizationList(requset, out int totalCount) ?? new List<TbOrgInfo>();

            if (list.Count == 0) throw new TransactionException("M01");

            response.TotalCount = totalCount;
            response.List = list.Select(x => new Organization
            {
                Address = x.Address,
                City = x.City,
                CreateTime = x.CreateTime.FormartDatetime(),
                District = x.District,
                OrgId = x.OrgId,
                OrgName = x.OrgName,
               
                Province = x.Province,
                Secretary = x.Secretary,
                ViceSecretary = x.ViceSecretary
            }).ToList();
            return response;
        }

        public GetOrganizationDetailResponse GetOrganizationDetail(GetOrganizationDetailRequset requset)
        {
            var response = new GetOrganizationDetailResponse
            {

            };
            TbOrgInfo detail = dataAccess.GetOrganizationDetail(requset);

            if (detail == null) throw new TransactionException("M01");

            response.Info = new OrganizationDetail
            {
                Address = detail.Address,
                City = detail.City,
                CreateTime = detail.CreateTime.FormartDatetime(),
                District = detail.District,
                OrgId = detail.OrgId,
                OrgName = detail.OrgName,
                Province = detail.Province,
                Logo = detail.Logo,
                EditTime = detail.EditTime,
                OperId = detail.OperId,
                Secretary=detail.Secretary,
                ViceSecretary=detail.ViceSecretary,
                ScoreType = detail.ScoreType,
                FeeExpireNoticeDay=detail.FeeExpireNoticeDay,
                FeeNotice=detail.FeeNotice
            };
            return response;
        }

        public GetOrganizationChartResponse GetOrganizationChart(GetOrganizationChartRequest request)
        {
            var response = new GetOrganizationChartResponse();
            var token = Cache.GetData<TbUserToken>(request.Token);
            TbOrgInfo detail = dataAccess.GetOrganizationDetail(token.OrgId);

            if (detail == null) throw new TransactionException("M01");
            response.Info = new OrganizationChart
            {
                OrgId = detail.OrgId,
                OrgName = detail.OrgName,
                Logo = detail.Logo,
                OrgSecretary = detail.Secretary,
                OrgViceSecretary = detail.ViceSecretary
            };
            var branchInfo = BrndataAccess.GetBranchByOrgId(token.OrgId);
            response.Info.BranchInfos = branchInfo.Select(x => new BranchInfo
            {
                BranchId = x.BranchId,
                BranchName = x.BranchName,
                BranchSecretary = x.Secretary,
                BranchViceSecretary = x.ViceSecretary,
                BranchUserCount = x.UserCount
            }).ToList();
            return response;
        }
    }
}